require(['grid', 'topBar', 'layout','select2'], function () {
    var editDialog, $top = $('#top'), $grid = $('#grid');
    var postData = {};
    $top.topBar({
        left: [{
                text: '所属机构',
                name: 'organId',
                option: {
                    url: '/baseOrgan/getOrganTree.htm',
                    name: 'organName',
                    idKey: 'organId',
                    pIdKey: 'parentOrganId'
                },
                width: 150,
                type: 'text-tree'
            },
            {
                text: '名称',
                name: 'fencName',
                width: 100,
                type: 'text'
            },
            {
                    text: '功能',
                    name: 'funcType',
                    type: 'select',
                    width: 80,
                    data: [
                        {text: '全部', value: ''},
                        {text: '围栏', value: 'FEN00001'},
                        {text: '投币机', value: 'FEN00003'},
                        {text: '异常区', value: 'FEN00004'}
                    ]
             },
            {
                text: '查询',
                type: 'btn',
                click: function () {
                        postData = $top.parent().getValues();
                        $grid.grid('setGridParam', {
                            datatype: "JSON",
                            page: 1,
                            postData: postData
                        }).trigger("reloadGrid");
                }
            }],
        right: [
            {
                text: '导出',
                type: 'export-btn',
                authority: '',
                item: [
                    {
                        text: 'Excel导出',
                        click: function () {
                            exportFile('Excel');
                        }
                    },
                    {
                        text: 'PDF导出',
                        click: function () {
                            exportFile('Pdf');
                        }
                    }
                ]
            },
            {
                text: '+添加',
                type: 'btn',
                authority: '',
                click: function () {
                    getEditDialog({title: '新增电子围栏'});
                }
            }
        ]
    });

    $('body').layout();



    $grid.grid({
        url: "/electFencSet/searchElectFenc.htm",
        colModel: [
            {name: "fencId", label: 'fencId', key: true, hidden: true},
            {name: "unBind", label: 'unBind', hidden: true},
            {name: "fencCd", label: '编号'},
            {name: "fencName", label: '名称'},
            {name: "organName", label: '机构名称'},
            {name: "fencType", label: '形状',
                formatter:function (cellvalue, options, rowObject){
                    if(rowObject.fencType=="1"){
                        return "多边形"
                    }else if(rowObject.fencType=="0"){
                        return "圆形"
                    }
                }},
            {name: "funcType", label: '功能'},
            {
                name: "operate", width: 130, label: '操作', align: 'center',
                formatter: 'operArea',
                formatoptions: [{
                        text: '查看',
                        css: 'operate-scan',
                        click: function (data) {
                            getEditDialog({title: '查看电子围栏'}).dialog('exec', 'getElectFence', data.fencId);
                        }
                    },{
                    text: '编辑',
                    css: 'operate-edit',
                    rule: function (data) {
                        return data.unBind == "0"
                    },
                    click: function (data) {
                        getEditDialog({title: '修改电子围栏'}).dialog('exec', 'getElectFence', data.fencId);
                    }
                }, {
                    text: '删除',
                    css: 'operate-delete',
                    rule: function (data) {
                        return data.unBind == "0"
                    },
                    click: function (data) {
                        $.messager.confirm('删除', '确定要删除该条记录吗？', 'delete', function (r) {
                            if (r) {
                                $.ajaxExt({
                                    url: "/electFencSet/deleteBaseElectFencById.htm",
                                    data: {'fencId': data.fencId}
                                }).done(function () {
                                    reloadGridData();
                                });
                            }
                        });
                    }
                }]
            }
        ]
    });

    /**
     * 获取表单对话框，如果没有就先创建
     * @param option {object} 对话框配置
     * @returns {jQuery}
     */
    function getEditDialog(option) {
        option = option || {};
        if (editDialog) {
            if (option.title) {
                editDialog.dialog('setOption', 'title', option.title);
            }

            editDialog.dialog('open');
            editDialog.dialog('exec', 'clearAll');
        } else {
            editDialog = $('<div>').dialog({
                title: option.title || '电子围栏设置',
                url: './service/setting/funElectronFenceMap.htm',
                height: $(window.parent).height() * 0.8,
                width: $(window.parent).width() * 0.8,
                autoOpen: true,
                buttons: [
                    {
                        text: '确定',
                        css: 'mr10',
                        click: function () {
                            var _t = this;
                            $(_t).dialog('exec', "saveOrUpdate", function () {
                                $(_t).dialog('close');
                                reloadGridData();
                            });
                        }
                    },
                    {
                        text: '重置',
                        css: 'btn-cancel',
                        click: function () {
                            $(this).dialog('exec', 'reset');
                        }
                    },{
                        text : '关闭',
                        css : 'btn-cancel',
                        click : function() {
                            $(this).dialog('close');
                        }
                    }
                ]
            });
        }
        if(option.title=='查看电子围栏'){
            editDialog.dialog('setOption','showBtn',[2]);
        }
        else{
            editDialog.dialog('setOption','showBtn',[0,1]);
        }
        return editDialog;
    }

    //查询grid
    function reloadGridData() {
        $grid.trigger("reloadGrid");
    }

    //导出
    function exportFile(suffix) {
        if ($grid.getGridParam("reccount") > 0) {
            var url = "/electFencSet/exportElectFenc" + suffix + ".htm";
            $.exportFile(url, postData);
        } else {
            $.messager.alert("警告", "请先查询数据！", "info");
        }
    }
});
